package com.zx.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.Reader;

public class SqlSessionFactoryUtil {
    private static SqlSessionFactory factory;
    private SqlSessionFactoryUtil(){}
    static{
        Reader reader = null;
        try{
            reader =
                    Resources.getResourceAsReader("mybatis.xml");
            factory =
                    new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        return factory. openSession(true);
    }

    private static ApplicationContext ac=new ClassPathXmlApplicationContext("springmvc.xml");
    public  static  ApplicationContext getAc(){
        return ac;
    }
}
